package managedBeans;

import interfacesRemotas.CategoriaRemote;
import interfacesRemotas.GenericDAORemote;

import java.util.ArrayList;
import java.util.List;

import javax.naming.InitialContext;

import util.Message;
import entityBeans.Categoria;


public class CategoriaMBean  {
	private Categoria categoria = new Categoria();

	//	@EJB(lookup="categoriaBean")
	private CategoriaRemote bean;

	private GenericDAORemote<Categoria> genericDaoBean;
	
	private List<Categoria> all = new ArrayList<Categoria>();
	InitialContext context;
	
	public CategoriaMBean() {
		try {
			context = new InitialContext();
			bean = (CategoriaRemote) context.lookup("CategoriaBean");
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Categoria getCategoria() {
		return categoria;
	}

	public void setCategoria(Categoria categoria) {
		this.categoria = categoria;
	}

	public void cadastrar() {
		try  {
			bean.cadastrar(categoria);
			Message.addMessage("cadastro_categoria", "Categoria cadastrada com sucesso!");

		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void setAll(List<Categoria> all) {
		this.all = all;
	}

	@SuppressWarnings("unchecked")
	public List<Categoria> getAll(){
		try{
			context = new InitialContext();
			genericDaoBean = (GenericDAORemote<Categoria>) context.lookup("GenericDAOBean");
			this.all = genericDaoBean.allEntities("Categoria");
			return this.all;
		}
		catch(Exception e){}
		return this.all;
	}

}
